package com.example.mydy.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.ProjectDetailActivity;
import com.example.mydy.R;
import com.example.mydy.model.Project;
import com.example.mydy.utils.ImageLoader;
import com.example.mydy.utils.MockDataManager;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder> {

    private Context context;
    private List<Project> projectList;
    private String currentUsername;
    private OnProjectActionListener listener;

    public interface OnProjectActionListener {
        void onUnlockButtonClick(Project project);
        void onProjectClick(Project project);
    }

    public ProjectAdapter(Context context, List<Project> projectList, String currentUsername, OnProjectActionListener listener) {
        this.context = context;
        this.projectList = projectList != null ? projectList : new ArrayList<>();
        this.currentUsername = currentUsername;
        this.listener = listener;
    }

    public void updateData(List<Project> newProjects) {
        this.projectList.clear();
        if (newProjects != null) {
            this.projectList.addAll(newProjects);
        }
        notifyDataSetChanged();
    }
    
    /**
     * 更新项目列表，作为updateData的别名
     * @param newProjects 新的项目列表
     */
    public void updateProjects(List<Project> newProjects) {
        updateData(newProjects);
    }

    public void filterByCategory(String category) {
        List<Project> allProjects = MockDataManager.getInstance().getProjects();
        List<Project> filteredProjects = new ArrayList<>();

        if (category.equals("all")) {
            filteredProjects.addAll(allProjects);
        } else if (category.equals("new")) {
            for (Project project : allProjects) {
                if (project.isNew()) {
                    filteredProjects.add(project);
                }
            }
        } else if (category.equals("hot")) {
            for (Project project : allProjects) {
                if (project.isHot()) {
                    filteredProjects.add(project);
                }
            }
        } else if (category.equals("unlocked")) {
            for (Project project : allProjects) {
                if (MockDataManager.getInstance().hasUnlockedProject(currentUsername, project.getId())) {
                    filteredProjects.add(project);
                }
            }
        }

        updateData(filteredProjects);
    }

    @NonNull
    @Override
    public ProjectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_project, parent, false);
        return new ProjectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProjectViewHolder holder, int position) {
        Project project = projectList.get(position);
        
        // 加载项目图片
        ImageLoader.loadImage(context, project.getImageUrl(), holder.projectImage);
        
        // 设置项目信息
        holder.projectTitle.setText(project.getTitle());
        holder.projectDescription.setText(project.getDescription());
        
        // 格式化价格显示
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.CHINA);
        String formattedPrice = currencyFormat.format(project.getPrice()).replace("CN¥", "¥");
        holder.projectPrice.setText(formattedPrice);
        
        // 检查项目是否已解锁
        boolean isUnlocked = MockDataManager.getInstance().hasUnlockedProject(currentUsername, project.getId());
        
        // 更新解锁状态
        if (isUnlocked) {
            holder.lockOverlay.setVisibility(View.GONE);
            holder.projectStatus.setText(R.string.unlocked);
            holder.projectStatus.setBackground(context.getDrawable(R.drawable.bg_unlocked_tag));
            holder.unlockButton.setText(R.string.view_details);
        } else {
            holder.lockOverlay.setVisibility(View.VISIBLE);
            holder.projectStatus.setText(R.string.unlock);
            holder.projectStatus.setBackground(context.getDrawable(R.drawable.bg_locked_tag));
            holder.unlockButton.setText(R.string.unlock);
        }
        
        // 显示是否为热门或新项目
        holder.tagHot.setVisibility(project.isHot() ? View.VISIBLE : View.GONE);
        holder.tagNew.setVisibility(project.isNew() ? View.VISIBLE : View.GONE);
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (isUnlocked) {
                if (listener != null) {
                    listener.onProjectClick(project);
                }
            } else {
                if (listener != null) {
                    listener.onUnlockButtonClick(project);
                }
            }
        });
        
        // 设置解锁/查看按钮点击事件
        holder.unlockButton.setOnClickListener(v -> {
            if (isUnlocked) {
                if (listener != null) {
                    listener.onProjectClick(project);
                }
            } else {
                if (listener != null) {
                    listener.onUnlockButtonClick(project);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return projectList.size();
    }

    static class ProjectViewHolder extends RecyclerView.ViewHolder {
        ImageView projectImage;
        TextView projectTitle, projectDescription, projectPrice, projectStatus;
        TextView tagHot, tagNew;
        FrameLayout lockOverlay;
        Button unlockButton;

        public ProjectViewHolder(@NonNull View itemView) {
            super(itemView);
            projectImage = itemView.findViewById(R.id.project_image);
            projectTitle = itemView.findViewById(R.id.project_title);
            projectDescription = itemView.findViewById(R.id.project_description);
            projectPrice = itemView.findViewById(R.id.project_price);
            projectStatus = itemView.findViewById(R.id.project_status);
            tagHot = itemView.findViewById(R.id.tag_hot);
            tagNew = itemView.findViewById(R.id.tag_new);
            lockOverlay = itemView.findViewById(R.id.lock_overlay);
            unlockButton = itemView.findViewById(R.id.btn_unlock_project);
        }
    }
}